package com.chat.transmission.util;

import cn.hutool.core.util.IdUtil;
import com.chat.common.common.constant.RedisKeyConstant;
import com.chat.common.dto.MessageDTO;
import com.chat.common.vo.MessageVO;
import com.google.gson.Gson;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wei kunJie
 * @date 2024/4/13 17:28:23
 */
@Component
public class RedisHashUtil {
    @Resource(name = "redisHashStringValue")
    private HashOperations<String, String, String> redisHashStringValue;
    @Resource(name = "redisHashObjectValue")
    private HashOperations<String, String, Object> redisHashObjectValue;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private Gson gson;

    /**
     * 消费通知
     *
     * @param consumerId
     * @return
     */
    public Map<String, String> consumeNotice(Long consumerId) {
        Map<String, String> entries = redisHashStringValue.entries(RedisKeyConstant.NOTICE + consumerId);
        deleteNotice(consumerId);
        return entries;
    }

    /**
     * 在收到客户端ack后删除缓存通知
     *
     * @param consumerId
     */
    public void deleteNotice(Long consumerId) {
        redisTemplate.delete(RedisKeyConstant.NOTICE + consumerId);
    }

    /**
     * 发送消息
     *
     * @param messageDTO
     */
    public void publishMessage(MessageDTO messageDTO) {
        messageDTO.setId(IdUtil.getSnowflakeNextId());//生成雪花id做为接收者用的id
        //从Redis拿userAccount
        String userAccount = (String) redisTemplate.opsForValue().get(RedisKeyConstant.TOKEN_ACCOUNT + messageDTO.getUserToken());
        if (messageDTO.getIsGroup()) {
            //群消息发送
            redisHashObjectValue.put(RedisKeyConstant.GROUP_MESSAGE_CACHE + messageDTO.getToUserAccount(),
                    RedisKeyConstant.MESSAGE_CACHE_FROM + getCurrentTimeMillisConstant() + userAccount,
                    messageDTO);
        } else {
            //通知的消费者id为key
            redisHashObjectValue.put(RedisKeyConstant.MESSAGE_CACHE + messageDTO.getToUserAccount(),
                    RedisKeyConstant.MESSAGE_CACHE_FROM + getCurrentTimeMillisConstant() + userAccount,
                    messageDTO);
        }
    }

    /**
     * 消费消息
     *
     * @return
     */
    public List<MessageVO> consumeMessage(String toUserAccount) {
        Map<String, Object> entries = redisHashObjectValue.entries(RedisKeyConstant.MESSAGE_CACHE + toUserAccount);
        List<MessageVO> messageVOList = new ArrayList<>();
        entries.forEach((k, v) -> {
            messageVOList.add(new MessageVO(k.split(":")[2], (MessageDTO) v));
        });
        deleteMessageCache(toUserAccount);
        return messageVOList;
    }

    /**
     * 获取群消息
     *
     * @param groupAccount
     * @return
     */
    public List<MessageVO> getGroupMessage(String groupAccount) {
        Map<String, Object> entries = redisHashObjectValue.entries(RedisKeyConstant.GROUP_MESSAGE_CACHE + groupAccount);
        List<MessageVO> messageVOList = new ArrayList<>();
        entries.forEach((k, v) -> {
            Long timeMillis = Long.valueOf(k.split(":")[1]);
            messageVOList.add(new MessageVO(timeMillis, k.split(":")[2],groupAccount, (MessageDTO) v));
        });

        return messageVOList;
    }

    /**
     * @return
     */
    public void deleteMessageCache(String toUserAccount) {
        redisTemplate.delete(RedisKeyConstant.MESSAGE_CACHE + toUserAccount);
    }

    public String getCurrentTimeMillisConstant() {
        return System.currentTimeMillis() + ":";
    }
}
